#include "HttpServer.hpp"
#include "Session.hpp"

using namespace LogModule;

void Login(HttpReauest &req, HttpResponse &resp)
{
    // 根据req动态构建resp
    LOG(LogLevel::DEBUG) << "进入登录模块" << req.Path() << ", " << req.Args();

    std::string req_args = req.Args();
    ssize_t pos = req_args.find("&");
    std::string username = req_args.substr(0, pos);
    std::string passwd = req_args.substr(pos + 1);

    session_ptr s = std::make_shared<Session>(username, true);
    std::string sessionid = _session_manager.AddSession(s);

    LOG(LogLevel::DEBUG) << username << " 被添加，sessionid是：" << sessionid;

    resp.SetHeader("Set-Cookie", "sessionid=" + sessionid + "; expires=" + ExpireTimeRfc1123(60000) + ";  path=/;"); // 让 cookie 1min 过期

    // 1. 解析参数格式，得到要的参数

    // 2. 访问数据库，验证合法用, 其他工作...
    // TODO

    // 3. 登陆成功
    std::string body = req.GetContent("wwwroot/login_success.html");
    resp.SetCode(200);
    resp.SetHeader("Content-Length", std::to_string(body.size()));
    resp.SetHeader("Content-Type", "text/html");
    resp.SetBody(body);
}

void Register(HttpReauest &req, HttpResponse &resp)
{
    // 根据req动态构建resp
    LOG(LogLevel::DEBUG) << "进入注册模块" << req.Path() << ", " << req.Args();

    if (!req.Islog())
    {
        std::string req_args = req.Args();

        std::string body = req.GetContent("wwwroot/register_success.html");
        resp.SetCode(200);
        resp.SetHeader("Content-Length", std::to_string(body.size()));
        resp.SetHeader("Content-Type", "text/html");
        resp.SetBody(body);
    }
}

void Search(HttpReauest &req, HttpResponse &resp)
{
    // 根据req动态构建resp
    LOG(LogLevel::DEBUG) << "进入搜索模块" << req.Path() << ", " << req.Args();
}

void IsLog(HttpReauest &req, HttpResponse &resp)
{
    // 根据req动态构建resp
    LOG(LogLevel::DEBUG) << "进入检测是否登录模块" << req.Uri();
    if (!req.Islog())
    {
        std::string body = req.GetContent("wwwroot/log.html");
        resp.SetCode(200);
        resp.SetHeader("Content-Length", std::to_string(body.size()));
        resp.SetHeader("Content-Type", "text/html");
        resp.SetBody(body);
    }
    else
    {
        // 3. 登陆成功 已经登陆过了就直接响应跳转到首页就行
        resp.SetCode(302);
        resp.SetHeader("Location", "/home.html");
    }
}

void IsRegister(HttpReauest &req, HttpResponse &resp)
{
    // 根据req动态构建resp
    if (!req.Islog())
    {
        std::string body = req.GetContent("wwwroot/register.html");
        resp.SetCode(200);
        resp.SetHeader("Content-Length", std::to_string(body.size()));
        resp.SetHeader("Content-Type", "text/html");
        resp.SetBody(body);
    }
    else
    {
        // 3. 登陆成功 已经登陆过了就直接响应跳转到首页就行
        resp.SetCode(302);
        resp.SetHeader("Location", "/home.html");
    }
}

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        std::cout << "Usage: " << argv[0] << " serverport" << std::endl;
        return 0;
    }
    auto httpserver = std::make_unique<HttpServer>(std::stoi(argv[1]));
    // 要让服务器具有登录功能
    httpserver->Resgiter("/login", Login);    // restful 风格的网络请求接口
    httpserver->Resgiter("/log.html", IsLog); // restful 风格的网络请求接口
    httpserver->Resgiter("/register", Register);
    httpserver->Resgiter("/register.html", IsRegister);
    httpserver->Resgiter("/search", Search);

    httpserver->Start();

    return 0;
}